package com.hansheng.action.chooseSheet;

import com.alibaba.fastjson.JSONArray;
import com.hansheng.service.impl.SheetServiceImpl;
import com.hansheng.util.JsonHelper;
import com.hansheng.util.PrintWriterHelper;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by GoldenMan on 2017/5/15.
 */
public class SheetFilterAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
    private SheetServiceImpl sheetService;
    private HttpServletResponse httpServletResponse;
    private HttpServletRequest httpServletRequest;
    private int mid;
    private int length;
    private int width;
    private int height;
    private int count;
    private int firstResult;
    private int maxResult;

    public int getFirstResult() {
        return firstResult;
    }

    public void setFirstResult(int firstResult) {
        this.firstResult = firstResult;
    }

    public int getMaxResult() {
        return maxResult;
    }

    public void setMaxResult(int maxResult) {
        this.maxResult = maxResult;
    }

    public int getMid() {
        return mid;
    }

    public void setMid(int mid) {
        this.mid = mid;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setSheetService(SheetServiceImpl sheetService) {
        this.sheetService = sheetService;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.httpServletRequest = request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.httpServletResponse = response;
        httpServletResponse.setContentType("text/html;charset=utf-8");
        httpServletResponse.setHeader("Cache-Control", "no-cache");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
    }

    @Override
    public String execute() throws Exception {
        String whereHql;
        Object[] params;
        if (getCount() > 1) {
            // Created by GoldenMan at 2017/5/16    TODO:improve
            whereHql = "where (o.mid=? or o.fatherMid=?) and o.length>=? and o.width>=? and o.height>=? and o.count>?";
            params = new Object[]{getMid(), getMid(), getLength(), getWidth(), getHeight(), getCount()};
        } else {
            whereHql = "where (o.mid=? or o.fatherMid=?) and o.length>=? and o.width>=? and o.height>=?";
            params = new Object[]{getMid(), getMid(), getLength(), getWidth(), getHeight()};
        }
        JSONArray resultArray = sheetService.getSheetByCondition(whereHql, params, getFirstResult(), getMaxResult() >= 0 ? getMaxResult() : 10);
        PrintWriterHelper.send(JsonHelper.successJSONBuilder(resultArray));
        return null;
    }
}
